const app = getApp();
import {
  imageUrl
} from "../../utils/imageutil.js";
import {
  provinces
} from '../../utils/provinces.js';
Page({
  data: {
    provinces: provinces,
    provinceIndex: -1,
    province: '',
    ageIndex: -1,
    age: '',
    byIndex: -1,
    imageUrl,

    _index: 0,
    sort: [{
        title: '离我最近',
        by: 4,
        desc: 1
      },
      {
        title: '星级排序',
        by: '6',
        desc: 1
      },
      {
        title: '薪资排序',
        by: 5,
        desc: 1
      }, {
        title: '综合排序',
        by: '',
        desc: 1,
      }
    ],

    navscurr: -1,
    navslist: [{
      title: '综合排序',
      id: 1
    }, {
      title: '籍贯',
      id: 2
    }, {
      title: '年龄',
      id: 3
    }, {
      title: '筛选',
      id: 4
    }],
    agelist: [{
      title: '不限',
      age: '',
      id: 1
    }, {
      title: '35岁以下',
      age: '1,35',
      id: 2
    }, {
      title: '36-40岁',
      age: '36,40',
      id: 3
    }, {
      title: '41-45岁',
      age: '41,45',
      id: 4
    }, {
      title: '46-50岁',
      age: '46,50',
      id: 5
    }, {
      title: '50岁以上',
      age: '51',
      id: 6
    }],
    bylist: [{
      title: '经验优先',
      id: 1,
      by: '1'
    }, {
      title: '好评优先',
      id: 2,
      by: '2'
    }, {
      title: '离我最近',
      id: 3,
      by: '4'
    }],
    titlenavcurr: 1,
    titlenavlist: [],
    housekeeperListPage: 1,
    housekeeperListType: 1,
    desc: 1,
    housekeeperListby: 4,
    housekeeperListFlag: true,
    housekeeperList: [],
    calendarHeight: 0,
    calendarHeightAndBtnH: 0,
    name: '',
    online: '',
    userInfo: wx.getStorageSync('userInfo'),
    type_show: true,
    aunt_type: "",
    city:""
  },
  onLoad: function (options) {
    if (options.type) {
      this.setData({
        type_show: false,
        aunt_type:options.type
      })
      wx.setNavigationBarTitle({
        title: options.type==1?"健康阿姨":"礼仪阿姨"
      })
    }
    if(options.city){
      console.log(options.city,'xixi');
      this.setData({
        city:options.city
      })
    }
    this.setData({
      userInfo: wx.getStorageSync('userInfo')
    })
    if (options.online) {
      this.setData({
        online: options.online
      })
    }
    this.getWorkType();
    this.gethousekeeperList();
    this.setData({
      calendarHeight: app.globalData.windowHeight - ((80 + 132) / 750 * app.globalData.windowWidth),
      calendarHeightAndBtnH: app.globalData.windowHeight - ((80 + 80) / 750 * app.globalData.windowWidth),
    })
  },

  checkby(e) {
    this.removby()
    let value = 'bylist[' + e.currentTarget.dataset.index + '].check'
    this.setData({
      [value]: !this.data.bylist[e.currentTarget.dataset.index].check,
      byIndex: e.currentTarget.dataset.index
    })
  },
  checkprovinces(e) {
    this.removeprovinces()
    let value = 'provinces[' + e.currentTarget.dataset.index + '].check'
    this.setData({
      [value]: !this.data.provinces[e.currentTarget.dataset.index].check,
      provinceIndex: e.currentTarget.dataset.index
    })
  },
  checkage(e) {
    this.removeage()
    let value = 'agelist[' + e.currentTarget.dataset.index + '].check'
    this.setData({
      [value]: !this.data.agelist[e.currentTarget.dataset.index].check,
      ageIndex: e.currentTarget.dataset.index,
    })
  },
  removby() {
    this.data.bylist.forEach((v, i) => {
      let value = 'bylist[' + i + '].check';
      this.setData({
        [value]: false,
        byIndex: -1,
      })
    })
  },
  removeprovinces() {
    this.data.provinces.forEach((v, i) => {
      let value = 'provinces[' + i + '].check';
      this.setData({
        [value]: false,
        provinceIndex: -1,
      })
    })
  },
  removeage() {
    this.data.agelist.forEach((v, i) => {
      let value = 'agelist[' + i + '].check';
      this.setData({
        [value]: false,
        ageIndex: -1
      })
    })
  },
  removeall() {
    this.removby()
    this.removeprovinces();
    this.removeage();
  },
  getby() { //综合排序
    this.setData({
      navscurr: -1,
      housekeeperListPage: 1,
      housekeeperListFlag: true,
      housekeeperList: []
    })
    if (this.data.byIndex == -1) {
      this.setData({
        housekeeperListby: ''
      })
    } else {
      this.setData({
        housekeeperListby: this.data.bylist[this.data.byIndex].by
      })
    }
    this.gethousekeeperList()
  },
  getprovince() { //籍贯
    this.setData({
      navscurr: -1,
      housekeeperListPage: 1,
      housekeeperListFlag: true,
      housekeeperList: []
    })
    if (this.data.provinceIndex == -1) {
      this.setData({
        province: ''
      })
    } else {
      this.setData({
        province: this.data.provinces[this.data.provinceIndex].title
      })
    }
    this.gethousekeeperList()
  },
  getage() { //年龄
    this.setData({
      navscurr: -1,
      housekeeperListPage: 1,
      housekeeperListFlag: true,
      housekeeperList: []
    })
    if (this.data.ageIndex == -1) {
      this.setData({
        age: ''
      })
    } else {
      this.setData({
        age: this.data.agelist[this.data.ageIndex].age
      })
    }
    this.gethousekeeperList()
  },
  getall() { //全部
    this.setData({
      navscurr: -1,
      housekeeperListPage: 1,
      housekeeperListFlag: true,
      housekeeperList: []
    })
    if (this.data.byIndex == -1) {
      this.setData({
        housekeeperListby: ''
      })
    } else {
      this.setData({
        housekeeperListby: this.data.bylist[this.data.byIndex].by
      })
    }
    if (this.data.provinceIndex == -1) {
      this.setData({
        province: ''
      })
    } else {
      this.setData({
        province: this.data.provinces[this.data.provinceIndex].title
      })
    }
    if (this.data.ageIndex == -1) {
      this.setData({
        age: ''
      })
    } else {
      this.setData({
        age: this.data.agelist[this.data.ageIndex].age
      })
    }
    this.gethousekeeperList()
  },

  to_aunt(e) {
    wx.navigateTo({
      url: '../aunt/index?uid=' + e.currentTarget.dataset.id + '&type=' + this.data.titlenavcurr,
    })
  },
  onReachBottom() {
    this.gethousekeeperList()
  },

  gethousekeeperList() {
    if (this.data.type_show) {
      // app.globalData.post({
      //   data: {
      //     do: 'housekeeperList',
      //     uid: wx.getStorageSync('userInfo').id,
      //     page: this.data.housekeeperListPage,
      //     by: this.data.housekeeperListby,
      //     desc: this.data.desc,
      //     pagesize: 10,
      //     type: this.data.housekeeperListType,
      //     age: this.data.age,
      //     province: this.data.province,
      //     name: this.data.name
      //   }
      // })
      getApp().globalData.post({
        url:"/api/applet.oyc/fx_ay_list",
        data:{
          cs:this.data.city,
          page:this.data.housekeeperListPage,
          keys:this.data.name
        }
      },true).then(res => {
        wx.stopPullDownRefresh();
        if (res.data.data == '') {
          this.setData({
            housekeeperListFlag: false
          })
        } else {
          let arr = this.data.housekeeperList.concat(res.data.data);
          this.setData({
            housekeeperList: arr,
            housekeeperListPage: this.data.housekeeperListPage + 1
          })
        }
      })
    }else{
      getApp().globalData.post({
        url:"/api/applet.oyc/ay_list.html",
        data:{
          type:this.data.aunt_type,
          page: this.data.housekeeperListPage,
          name: this.data.name
        }
      },true).then(res => {
        wx.stopPullDownRefresh();
        if (res.data.data.data == '') {
          this.setData({
            housekeeperListFlag: false
          })
        } else {
          let arr = this.data.housekeeperList.concat(res.data.data.data);
          this.setData({
            housekeeperList: arr,
            housekeeperListPage: this.data.housekeeperListPage + 1
          })
        }
      })
    }

  },
  getWorkType() {
    app.globalData.post({
      data: {
        do: 'WorkType',
        online: this.data.online
      }
    }).then(res => {
      this.setData({
        titlenavlist: res.data.data
      })
    })
  },
  changetitlenav(e) {
    this.setData({
      titlenavcurr: e.currentTarget.dataset.id
    })
    if (this.data.titlenavcurr == this.data.housekeeperListType) {

    } else {
      this.setData({
        housekeeperListPage: 1,
        housekeeperListType: this.data.titlenavcurr,
        housekeeperListFlag: true,
        housekeeperList: [],
      })
      this.gethousekeeperList();
    }
  },

  sortchange(e) { //排序切换
    let by = e.currentTarget.dataset.by;
    let desc = this.data.sort[e.currentTarget.dataset.index].desc
    if (this.data.housekeeperListby == by) {
      desc = desc == 1 ? 2 : 1
    }
    this.setData({
      _index: e.currentTarget.dataset.index,
      housekeeperListby: by,
      housekeeperListPage: 1,
      housekeeperList: [],
      desc: desc,
      ['sort[' + e.currentTarget.dataset.index + '].desc']: desc
    })
    this.gethousekeeperList()
  },

  changenav(e) {
    this.setData({
      navscurr: this.data.navscurr == e.currentTarget.dataset.id ? -1 : e.currentTarget.dataset.id
    })
  },
  changekeywords(e) { //搜索家政员
    this.setData({
      name: e.detail.value,
      housekeeperListPage: 1,
      housekeeperListFlag: true,
      housekeeperList: [],
    })
    this.gethousekeeperList()
  },

  to_store(e) {
    wx.navigateTo({
      url: '../housekeeping/index?store_id=' + e.currentTarget.dataset.store_id,
    })
  },

})